
<template>
  <div class="chat-list-history">
    <div class="chat-list-history-item-wrapper" v-for="item in profileList" :key="item.id">
      <div
          @click="handleCheckItem(item)"
          :class="['chat-list-history-item', checkedHistory.customer.id === item.customer.id ? 'active' : '']">
        <!-- 这是一个历史列表，左侧是当前用户头像和最近聊天时间，上下布局，有一个圆点表示在线状态，红色离线，红色在线，相对头像右下角定位；中间是当前用户名称和聊天信息，上下布局；右侧是好友头像和姓名，左右布局 -->
        <div class="chat-list-history-left">
          <div class="profile-avatar">
            <el-image class="avatar ng-star-inserted" alt="Avatar" :src="item.profile.photo" style="width: min(3.42857rem, 2.5vw); height: min(3.42857rem, 2.5vw);" />
            <div class="profile-status">
              <div :class="['profile-status-value', item.profile.online ? 'online' : 'offline']" ></div>
            </div>
          </div>
          <div class="chat-list-history-left-time">{{ item.createdDate | formatDate }}</div>
        </div>
        <div class="chat-list-history-middle">
          <div class="chat-list-history-middle-name">{{ item.profile.name}}</div>
          <div class="chat-list-history-middle-content">
            <img class="svg-icon height_22px" v-if="item.contentType === enumType.MAIL" src="~@/assets/mail_white.svg" alt="" />
            <div class="chat-list-history-middle-content-text" v-if="item.contentType === enumType.MAIL">Mail</div>
            <div class="chat-list-history-middle-content-text" v-if="item.contentType === enumType.MESSAGE">{{ item.content.message }}</div>
            <el-image class="STICKER" v-if="item.contentType === enumType.STICKER" :src="item.content.url" alt="" />
            <el-image class="STICKER" v-for="photo in item.content.photos" v-if="item.contentType === enumType.PHOTO_BATCH" :src="photo.url" alt="" />
          </div>
        </div>
        <div class="chat-list-history-right">
          <img :src="item.customer.photo" alt="">
          <div class="chat-list-history-right-name">{{ item.customer.name}}</div>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
import {mapGetters} from "vuex";
import {formatDate} from "@/utils";

export default {

  filters: {
    formatDate: formatDate
  },
  data() {
    return {
      profileList: [
      ],
      checkedHistory: {
        customer: {}
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    assignedProfiles() {
      return (this.userInfo.assignedProfiles || [])
    }
  },
  created() {
    // 获取历史聊天
    this.getHistoryList()
  },
  methods: {
    // 请求接口获取历史聊天
    getHistoryList() {
      if (this.assignedProfiles.length === 0) return
      this.$http.getChatHistoryList({
        page: '1',
        profile: this.assignedProfiles[0].id
      }).then(data => {
        this.handleHistoryData(data)
      }).catch(() => {
        let data = [
          {
            "profileId": "pd-68365636",
            "customer": {
              "id": "77197995",
              "name": "Steven",
              "photo": "https://i.gstatvb.com/shpzkl28svdhdhukp.r100x100.8a99b09c.jpg"
            },
            "subject": "",
            "content": {
              "message": "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest"
            },
            "contentType": "MAIL",
            "createdDate": "2023-08-20 14:55:44.044Z"
          },
          {
            "profileId": "pd-68365636",
            "customer": {
              "id": "83341402",
              "name": "Kevin",
              "photo": "https://i.gstatvb.com/shpzkl3gqq7lmq388.r100x100.417404e5.jpg"
            },
            "content": {
              "photos": [
                {
                  "@type": "ImageDto",
                  "id": 25619002,
                  "url": "https://i.gstatvb.com/shpzkl341d6ivqpnu.045a8402.jpg",
                  "urlThumbnail": "https://i.gstatvb.com/shpzkl341d6ivqpnu.r170x170.577054fe.jpg"
                }
              ]
            },
            "contentType": "PHOTO_BATCH",
            "createdDate": "2023-08-20 12:59:38.531Z"
          },
          {
            "profileId": "pd-68365636",
            "customer": {
              "id": "57252898",
              "name": "ralph mcalister",
              "photo": "https://i.gstatvb.com/shpzkl70032tvj243.r100x100.95cbeaba.jpg"
            },
            "content": {
              "message": "dadadsaadsdadsdadadadadadsaadsdadsdadadadadadsaadsdadsdadadadadadsaadsdadsdadadadadadsaadsdadsdadadadadadsaadsdadsdadadadadadsaadsdadsdadadadadadsaadf"
            },
            "contentType": "MESSAGE",
            "createdDate": "2023-08-20 12:53:09.233Z"
          },
          {
            "profileId": "pd-68365636",
            "customer": {
              "id": "11189995",
              "name": "Gary",
              "photo": "https://i.gstatvb.com/shpzkl35dnr15rhg6g.r100x100.1445582d.jpg"
            },
            "content": {
              "message": "212121"
            },
            "contentType": "MESSAGE",
            "createdDate": "2023-08-17 18:54:57.797Z"
          },
          {
            "profileId": "pd-68365636",
            "customer": {
              "id": "39634514",
              "name": "Jake",
              "photo": "https://i.gstatvb.com/shpzkl472cpm27u8a.c1106x1474x228x119.r100x100.41df19c7.jpg"
            },
            "content": {
              "photos": [
                {
                  "@type": "ImageDto",
                  "id": 25618938,
                  "url": "https://i.gstatvb.com/shpzkl20o2qc8arnv.264efdb2.jpg",
                  "urlThumbnail": "https://i.gstatvb.com/shpzkl20o2qc8arnv.r170x170.b53b3336.jpg"
                }
              ]
            },
            "contentType": "PHOTO_BATCH",
            "createdDate": "2023-08-17 18:44:13.269Z"
          },
          {
            "profileId": "pd-68365636",
            "customer": {
              "id": "85053874",
              "name": "Ray",
              "photo": "https://i.gstatvb.com/shpzklb22a37j80o4.r100x100.c121db3d.jpg"
            },
            "content": {
              "message": "2121"
            },
            "contentType": "MESSAGE",
            "createdDate": "2023-08-16 17:47:29.233Z"
          }
        ]
        this.handleHistoryData(data)
      })
    },
    handleHistoryData(data) {
      this.profileList = data.map(el => {
        return {
          ...el,
          profile: this.assignedProfiles.find(el2 => el2.id === el.profileId) || {}
        }
      })
    },
    handleCheckItem(item) {
      this.checkedHistory = item
      this.$emit('change', {
        type: item.contentType === this.enumType.MAIL ? 'MAIL' : 'CHAT',
        profileId: item.profileId,
        customerId: item.customer.id
      })
    }
  }
}
</script>

<style scoped lang="scss">
.chat-list-history {
  .chat-list-history-item-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.286rem;
    justify-content: center;
    width: 100%;
    margin-bottom: 0.143rem;
  }
  .chat-list-history-item {
    display: flex;
    cursor: pointer;
    gap: 0.571rem;
    align-items: center;
    border-radius: 0.571rem;
    background-color: #193343;
    padding: min(0.571rem,0.417vw);
    text-shadow: 2px 1px 3px rgba(173,154,153,.27);
    &:active,
    &:hover {
      background-color: #1d3b4e;
    }
  }
  .chat-list-history-left {
    display: flex;
    flex-direction: column;
    gap: min(0.286rem,0.208vw);
    justify-content: center;
    .profile-avatar {
      position: relative;
      margin-right: min(calc(4 / 14 * 1rem),calc(4 / (1920 / 100) * 1vw));
      img {
        border-radius: min(calc(8 / 14 * 1rem),calc(8 / (1920 / 100) * 1vw));
        object-fit: cover;
      }
      .profile-status {
        position: absolute;
        right: 0;
        bottom: 1px;
        width: 1rem;
        height: 1rem;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: #0E222E;
        .profile-status-value {
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%,-50%);
          width: 66%;
          height: 66%;
          cursor: pointer;
          border-radius: 50%;
        }
      }
    }
    .chat-list-history-left-time {
      color: #fff;
      opacity: .5;
      line-height: 150%;
      font-size: 0.643rem;
      white-space: nowrap;
      width: 3.143rem;
      max-width: 3.143rem;
      text-align: center;
    }
  }
  .chat-list-history-middle {
    display: flex;
    flex-direction: column;
    align-self: flex-start;
    gap: min(0.571rem,0.417vw);
    width: 12.5vw;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    .chat-list-history-middle-name {
      display: flex;
      gap: 0.286rem;
      color: #dfdfdf;
      font-weight: 700;
      font-size: min(.929rem,.677vw);
    }
    .chat-list-history-middle-content {
      color: #fff;
      font-weight: 400;
      line-height: 150%;
      text-align: justify;
      opacity: .5;
      white-space: break-spaces;
      width: 12.5vw;
      display: flex;
      align-items: center;
      .chat-list-history-middle-content-text {
        margin-left: 4px;
      }
      .STICKER {
        max-width: 2.643rem;
        max-height: 2.643rem;
      }
    }

  }
  .chat-list-history-right {
    display: grid;
    grid-template-columns: min(2.714rem,1.979vw) 1fr auto;
    align-items: center;
    justify-items: center;
    padding: 0.286rem;
    gap: min(calc(10 / 14 * 1rem),calc(10 / (1920 / 100) * 1vw));
    min-width: 6.927vw;
    width: 6.927vw;
    height: min(3.429rem,2.5vw);
    margin-left: auto;
    background: #122A3A;
    border-radius: 0.571rem;
    img {
      object-fit: cover;
      border-radius: 0.571rem;
      width: min(2.714rem,1.979vw);
      height: min(2.714rem,1.979vw);
    }
    .chat-list-history-right-name {
      color: #d9d1d1;
      font-weight: 700;
      overflow: hidden;
      text-overflow: ellipsis;
      width: 100%;
      font-size: .75rem;
      justify-self: start;
      white-space: nowrap;
    }
  }
}
</style>